package com.jic.product.controller.store;

import com.jic.common.base.vo.RestResult;
import com.jic.product.model.entity.File;
import com.jic.product.model.entity.MStore;
import com.jic.product.model.entity.ProductComment;
import com.jic.product.service.FileService;
import com.jic.product.service.MStoreManageService;
import com.jic.product.service.ProductCommentService;
import com.jic.product.store.MStoreApi;
import com.jic.product.store.request.*;
import com.jic.product.store.response.MStoreResponse;
import com.jic.product.store.response.ProductCommentResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 建投数据科技(山东)有限公司
 * @date: 2020-04-24 16:04:24
 */
@RestController
@Slf4j
@RequestMapping("/mStoreManage")
@Api(tags = " [仓库中心]对外管理类-PC")
public class MStoreManageController implements MStoreApi{
    @Autowired
    private MStoreManageService service;

    @Autowired
    private ProductCommentService productCommentService;

    @Autowired
    private FileService fileService;

    /**
     * [仓库中心]订单冻结
     *
     * @param request 订单冻结 请求参数
     * @return MStoreResponse 订单冻结响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-4-28 10:31:35
     */
    @ApiOperation(value = "[仓库中心]订单冻结")
    @PostMapping("/frozen")
    @ResponseBody
    public RestResult<Boolean> frozen(@RequestBody MStoreFrozenRequest request) {
        return service.frozen(request);
    }

    /**
     * [仓库中心]订单提交
     *
     * @param request 订单提交请求参数
     * @return MStoreResponse 订单提交响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-4-28 10:31:35
     */
    @ApiOperation(value = "[仓库中心]保存或更新[实体仓库表]")
    @PostMapping("/submit")
    @ResponseBody
    public RestResult<Boolean> submit(@Validated @RequestBody MStoreSubmitRequest request) {
        RestResult<Boolean> submit = service.submit(request);
        return submit;
    }

    /**
     * [仓库中心]根据商品Id,获取所有包含该商品的实体仓;
     *
     * @param request 请求参数
     * @return MStoreResponse 响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-4-28 10:31:35
     */
    @ApiOperation(value = "[仓库中心]根据商品Id,获取所有包含该商品的实体仓")
    @PostMapping("/listMStoreByProductId")
    @ResponseBody
    public RestResult<List<MStoreResponse>> listMStoreInfoByOrderInfo(@RequestBody MStoreQueryRequest request) {
        List<MStore> list = service.listMStoreInfoByOrderInfo(request);
        List<MStoreResponse> responseList = list.stream().map(temp -> {
            MStoreResponse bo = new MStoreResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }
    /**
     * [仓库中心]获取可售数量最多的实体仓列表;
     * @param request 请求参数
     * @return MStoreResponse 响应参数
     * author taozhouchuan
     * @date 2020-4-28 20:14:23
     */
    @ApiOperation(value = "[仓库中心]获取最大可售的实体仓列表")
    @PostMapping("/maxAvailable")
    @ResponseBody
    public RestResult<MStoreResponse> listMStoreOfMaxAvailable(@RequestBody MStoreQueryRequest request) {
        MStore mStore = service.listMStoreOfMaxAvailable(request);
        MStoreResponse response = new MStoreResponse();
        BeanUtils.copyProperties(mStore,response);
        return RestResult.success(response);
    }

    /**
     * [仓库中心]获取当天发货量最少的的实体仓列表;
     * @param request 请求参数
     * @return MStoreResponse 响应参数
     * author taozhouchuan
     * @date 2020-4-28 20:14:23
     */
    @ApiOperation(value = "[仓库中心]获取当天最小发货的实体仓列表")
    @PostMapping("/minDelivery")
    @ResponseBody
    public RestResult<MStoreResponse> listStoreOfMinDelivery(@RequestBody MStoreQueryRequest request) {
        MStore mStore = service.listMStoreOfMaxAvailable(request);
        MStoreResponse response = new MStoreResponse();
        BeanUtils.copyProperties(mStore,response);
        return RestResult.success(response);
    }

    /**
     * 店铺收货后同步库存方法
     * @param request
     * @return
     */
    @Override
    @PostMapping("/syncStock")
    @ResponseBody
    public RestResult<Boolean> syncStock( @RequestBody MStoreShopSyncStockRequest request) {
        Boolean flag = service.syncStock(request);
        return RestResult.success(flag);
    }

    /**
     * 根据商家code得到一个实体仓信息
     * @param request
     * @return
     */
    @Override
    public RestResult<com.jic.product.store.request.MStore> getStoreByMerchantCode(MStoreReq request) {

        return RestResult.success(service.getStoreByMerchantCode(request));
    }

    @ApiOperation("根据子单编号查询评论")
    @ResponseBody
    @PostMapping("getProductCommentByOrderNo")
    @Override
    public RestResult<ProductCommentResponse> getProductCommentByOrderNo(@RequestBody ProductCommentRequest productCommentRequest){
        List<ProductComment> productComments = productCommentService.query(new ProductComment() {{
            setOrderNo(productCommentRequest.getOrderNo());
            setProductId(productCommentRequest.getProductId());
        }});
        List<ProductCommentResponse> resList = new ArrayList<>();
        productComments.forEach(productComment -> {
            ProductCommentResponse productCommentResponse = new ProductCommentResponse();
            BeanUtils.copyProperties(productComment,productCommentResponse);
            List<File> files = fileService.query(new File() {{
                setType(0);
                setBusinessId(productComment.getId());
            }});
            productCommentResponse.setFileList(files);
            resList.add(productCommentResponse);
        });
        Map<Integer, List<ProductCommentResponse>> commentResList = resList.stream().collect(Collectors.groupingBy(productCommentResponse -> productCommentResponse.getType()));
        if (commentResList != null){
            List<ProductCommentResponse> productCommentResponses = commentResList.get(1);
            if (CollectionUtils.isNotEmpty(productCommentResponses)){
                ProductCommentResponse productCommentResponse = productCommentResponses.get(0);
                productCommentResponse.setChildren(commentResList.get(2) == null ? new ArrayList<>() : commentResList.get(2));
                return RestResult.success(productCommentResponse);
            }
        }
        ProductCommentResponse productCommentResponse = new ProductCommentResponse();
        productCommentResponse.setChildren(new ArrayList<>());
        return RestResult.success(productCommentResponse);
    }


}

